package com.demo.apparel.controller.uniapp;

import com.demo.apparel.common.Result;
import com.demo.apparel.dto.uniapp.*;
import com.demo.apparel.service.uniapp.WxUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/wx/user")
@Tag(name = "微信用户接口", description = "微信小程序用户登录接口")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService ;

    @PostMapping("/login")
    @Operation(summary = "微信一键登录接口")
    public Result<WxUserDTO> login(@Valid @RequestBody WxLoginDTO loginDTO) {
        WxUserDTO userDTO = wxUserService.login(loginDTO);
        return Result.success(userDTO);
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    public Result<WxUserDTO> updateUser(
            @RequestAttribute("userId") String userId,
            @RequestBody WxUpdateDTO updateDTO,
            HttpServletRequest request
    ) {
        WxUserDTO updatedUser = wxUserService.updateUser(userId, updateDTO);

        // 取出原始 token（不重新生成）
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null && authHeader.startsWith("Bearer ")) {
            updatedUser.setToken(authHeader.substring(7)); // 去掉 Bearer
        }

        return Result.success(updatedUser);
    }

    //一键登录】
    @PostMapping("/phoneLogin")
    @Operation(summary = "微信手机号登录")
    public Result<WxUserDTO> phoneLogin(@RequestBody WxPhoneLoginDTO phoneLoginDTO) {
        WxUserDTO userDTO = wxUserService.loginWithPhone(phoneLoginDTO);
        return Result.success(userDTO);
    }

    /**
     *
     * @param file
     * @return
     */
    @PostMapping("/upload/avatar")
    @Operation(summary = "上传单个头像")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file,
                                       HttpServletRequest request) {
        // 从请求的 token 里解析 userId
        String userId = (String) request.getAttribute("userId");
        String avatarUrl = wxUserService.uploadAvatar(file, userId);
        return Result.success(avatarUrl);
    }

//    @PostMapping("/register/h5")
//    @Operation(summary = "H5 注册接口（手机号+密码）")
//    public Result<WxUserDTO> registerH5(@RequestParam String phone,
//                                        @RequestParam String password) {
//        WxUserDTO userDTO = wxUserService.registerH5(phone, password);
//        return Result.success(userDTO);
//    }

    @PostMapping("/register/h5")
    @Operation(summary = "H5 注册接口（手机号+密码）")
    public Result<WxUserDTO> registerH5(@RequestBody WxUserRegisterDTO dto) {
        WxUserDTO userDTO = wxUserService.registerH5(dto.getPhone(), dto.getPassword());
        return Result.success(userDTO);
    }


    @PostMapping("/login/h5")
    @Operation(summary = "H5 登录接口（手机号+密码）")
    public Result<WxUserDTO> loginH5(@RequestParam String phone,
                                     @RequestParam String password) {
        WxUserDTO userDTO = wxUserService.loginH5(phone, password);
        return Result.success(userDTO);
    }

}

